package zcw.com.lib_leet_code;

import java.util.Arrays;
import java.util.List;

import zcw.com.lib_leet_code.bean.TreeNode;
import zcw.com.lib_leet_code.util.Util;

/**
 * Created by 朱城委 on 2021/8/9.<br><br>
 *
 * 简单：二叉搜索树的最小绝对差
 */
public class Topic530 {

    public static void main(String[] args) {
        Topic530 instance = new Topic530();

        List<Integer> list = Arrays.asList(4,2,6,1,3);
        TreeNode root = Util.createTree(list);
        System.out.println(instance.getMinimumDifference(root));

        instance.init();
        list = Arrays.asList(1,0,48,null,null,12,49);
        root = Util.createTree(list);
        System.out.println(instance.getMinimumDifference(root));
    }

    private int preValue = Integer.MAX_VALUE;
    private int result = Integer.MAX_VALUE;

    public int getMinimumDifference(TreeNode root) {
        inorder(root);
        return result;
    }

    private void inorder(TreeNode root) {
        if(root == null) {
            return ;
        }

        inorder(root.left);

        int temp = Math.abs(root.val - preValue);
        if(temp < result) {
            result = temp;
        }
        preValue = root.val;

        inorder(root.right);
    }

    private void init() {
        preValue = Integer.MAX_VALUE;
        result = Integer.MAX_VALUE;
    }
}
